package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminAddressServiceImpl implements AdminAddressService {

    @Override
    public List<MarketAddress> list(final String page, final String limit, final String sort, final String order, final String userId, final String name) {
        SqlSession session = MybatisUtils.getSession();
        MarketAddressMapper marketAddressMapper = session.getMapper(MarketAddressMapper.class);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();

        if(!StringUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }

        marketAddressExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketAddress> marketAddressList = marketAddressMapper.selectByExample(marketAddressExample);
        session.commit();
        session.close();
        return marketAddressList;
    }
}
